<template>
  <el-dialog :visible.sync="visible" title="详情" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-col :span="24">
      <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm"
        :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'">
        <el-col :span="24">
          <!-- sql 字符串 -->
          <el-form-item label="sql" prop="sqlStr">
            <!--文本框-->
            <el-input :disabled="true" type="textarea" :autosize="{ minRows: 2, maxRows: 4}" v-model="dataForm.sqlStr"
              placeholder="sql 字符串" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 执行状态 -->
          <el-form-item label="执行状态" prop="executionStatus">
            <!--文本框-->
            <el-input :disabled="true" v-model="dataForm.executionStatus" placeholder="执行状态" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 执行影响行数 -->
          <el-form-item label="执行影响行数" prop="executeRows">
            <!--文本框-->
            <el-input :disabled="true" v-model="dataForm.executeRows" placeholder="执行影响行数" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 执行时长 -->
          <el-form-item label="执行时长" prop="executeDuration">
            <!--文本框-->
            <el-input :disabled="true" v-model="dataForm.executeDuration" placeholder="执行时长" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 最慢时长 -->
          <el-form-item label="最慢时长" prop="slowDuration">
            <!--文本框-->
            <el-input :disabled="true" v-model="dataForm.slowDuration" placeholder="最慢时长" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 事务影响行数 -->
          <el-form-item label="事务影响行数" prop="transactionalRows">
            <!--文本框-->
            <el-input :disabled="true" v-model="dataForm.transactionalRows" placeholder="事务影响行数" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 错误行数 -->
          <el-form-item label="错误行数" prop="errorRows">
            <!--文本框-->
            <el-input :disabled="true" v-model="dataForm.errorRows" placeholder="错误行数" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 更新行数 -->
          <el-form-item label="更新行数" prop="updateRows">
            <!--文本框-->
            <el-input :disabled="true" v-model="dataForm.updateRows" placeholder="更新行数" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 读取行数 -->
          <el-form-item label="读取行数" prop="readRows">
            <!--文本框-->
            <el-input :disabled="true" v-model="dataForm.readRows" placeholder="读取行数" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 最大并发数 -->
          <el-form-item label="最大并发数" prop="maxConcurrent">
            <!--文本框-->
            <el-input :disabled="true" v-model="dataForm.maxConcurrent" placeholder="最大并发数" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-col>
    <template slot="footer">
      <el-button @click="visible = false">关闭</el-button>
      <!-- <el-button type="primary" :loading="loading" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button> -->
    </template>
  </el-dialog>
</template>
<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      loading: false,
      visible: false,
      dataForm: {
        id: '',
        sqlStr: '',
        executionStatus: '',
        executeRows: '',
        executeDuration: '',
        slowDuration: '',
        transactionalRows: '',
        errorRows: '',
        updateRows: '',
        readRows: '',
        maxConcurrent: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {}
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.loading = true
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/sys/sysLogSql/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => { })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        this.$http[!this.dataForm.id ? 'post' : 'put']('/sys/sysLogSql/', this.dataForm).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return false
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
